<?php

namespace Api\Library;

class IMPack {

    /**
     * 数据打包
     * @param $head  消息头
     * @param $body  消息体
     * @param $attachment 附件
     * @author xulihua 2014-06-27
     */
    public static function createPacket($head, $body, $attachment) {
        $len_head = strlen($head);
        $len_body = strlen($body);
        $len_attachment = strlen($attachment);
        $len_all = 12 + $len_head + $len_body + $len_attachment;
        $data = pack("N4", $len_all, $len_head, $len_body, $len_attachment);
        $data .= $head;
        $data .= $body;
        $data .= $attachment;
        return $data;
    }

    /**
     * 数据解包
     * @return 成功 0
     * @param $in  输入数据包
     * @param $head  返回的消息头
     * @param $body  返回的消息体
     * @param $attachment 返回的附件
     * @author xulihua 2014-06-27
     */
    public static function unPacket($in, &$head, &$body, &$attachment) {
        $len = unpack("N4", $in);
        $len_head = $len[2];
        $len_body = $len[3];
        $len_attachment = $len[4];
        if ($len_head > 0) {
            $head = substr($in, 16, $len_head);
        }
        if ($len_body > 0) {
            $body = substr($in, 16 + $len_head, $len_body);
        }
        if ($len_attachment > 0) {
            $attachment = substr($in, 16 + $len_head + $len_body, $len_attachment);
        }
        return 0;
    }

    /**
     * 发送数据解包，返回值为返回数据包
     * @return 返回的数据包
     * @param $ip    服务器ip
     * @param $port  端口号
     * @param $data  要发送的数据包
     * @author xulihua 2014-06-27
     */
    public static function sendMsg2($ip, $port, $data) {
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if ($socket === false) {
            return -1;
        }

        $result = socket_connect($socket, $ip, $port);
        if ($result === false) {
            return -2;
        }
        socket_write($socket, $data, strlen($data));
        $retData = socket_read($socket, 4);
        if (strlen($retData) < 4) {
            return -3;
        }
        $len = unpack("N1", $retData);
        $retData .= socket_read($socket, $len[1]);
        socket_close($socket);
        return $retData;
    }

    /**
     * 发送数据解包，返回值为返回数据包
     * @return 返回的数据包
     * @param $ip    服务器ip
     * @param $port  端口号
     * @param $head  返回的消息头
     * @param $body  返回的消息体
     * @param $attachment 返回的附件
     * @author xulihua 2014-06-27
     */
    public static function sendMsg($ip, $port, $head, $body, $attachment) {
        $in = IMPack::createPacket($head, $body, $attachment);
        return IMPack::sendMsg2($ip, $port, $in);
    }

    /**
     * 创建包头
     * @return 返回的包头json
     * @param $id    
     * @param $code  		命令码
     * @param $oneway  
     * @param $extFields  	内容
     * @author xulihua 2014-10-25
     */
    public static function createPackHead($id, $code, $oneway, $extFields) {
        $arrayHead = array('id' => $id, 'code' => $code, 'oneway' => $oneway, 'extensionFields' => $extFields);
        return json_encode($arrayHead, JSON_FORCE_OBJECT);
    }

    /**
     * 创建包体
     * @return 返回的包体json
     * @param $title  	标题
     * @param $info  	内容
     * @param $action  	行为
     * @param $extended 扩展
     * @author xulihua 2014-10-25
     */
    public static function createPackBody($title, $info, $action, $extended) {
        $arrayBody = array('title' => $title, 'info' => $info, 'action' => $action, 'extended' => $extended);
        return json_encode($arrayBody, JSON_FORCE_OBJECT);
    }

    /*
      //文本
      @property (strong, nonatomic) NSString *text;                     //文本消息内容

      //文件
      @property (strong, nonatomic) NSString *fileUrl;                  //文件下载地址
      @property (strong, nonatomic) NSString *filePath;                 //文件本地路径
      @property (strong, nonatomic) NSString *fileName;                 //文件名称
      @property (strong, nonatomic) NSString *fileType;                 //文件类型
      @property (strong, nonatomic) NSString *fileSize;                 //文件大小
     */

    public static function createChatMsgBody($text, $fileUrl, $filePath, $fileName, $fileType, $fileSize) {
        $arrayBody = array('text' => $text, 'fileUrl' => $fileUrl, 'filePath' => $filePath, 'fileName' => $fileName, 'fileType' => $fileType, 'fileSize' => $fileSize);
        return json_encode($arrayBody, JSON_FORCE_OBJECT);
    }

}
